package com.ruoyi.web.controller.module.platform;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.platform.PlatformSplit;
import com.ruoyi.system.service.system.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.platform.IPlatformSplitService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 分成比例Controller
 *
 * @author ruoyi
 * @date 2021-08-17
 */
@Validated
@Api(value = "分成比例控制器", tags = {"分成比例管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/split")
public class PlatformSplitController extends BaseController {

    private final IPlatformSplitService platformSplitService;
    private final ISysDictDataService sysDictDataService;

    /**
     * 查询分成比例列表
     */
    @ApiOperation("查询分成比例列表")
    @PreAuthorize("@ss.hasPermi('module:split:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformSplit> list(@Validated PlatformSplit platformSplit) {
        TableDataInfo<PlatformSplit> td = PageUtils.buildDataInfo(platformSplitService.page(PageUtils.buildPage()));
        List<PlatformSplit> list = td.getRows();
        list.forEach(split->{
			SysDictData dictData = sysDictDataService.selectDictData("split_type",split.getSplitType());
			String content = "：业主：" + split.getProprietorSplit() + split.getSplitTypeUnit();
			content += ", 物业：" + split.getPropertySplit() + split.getSplitTypeUnit();
			content += "，平台：" + split.getPlatformSplit() + split.getSplitTypeUnit();
			split.setSplitTypeName(dictData.getDictLabel() + content);
			split.setSplitTypeUnit(dictData.getCssClass());

        });
        return td;
    }

    /**
     * 获取分成比例详细信息
     */
    @ApiOperation("获取分成比例详细信息")
    @PreAuthorize("@ss.hasPermi('module:split:query')")
    @GetMapping("/{id}")
    public AjaxResult<PlatformSplit> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(platformSplitService.getById(id));
    }

    /**
     * 新增分成比例
     */
    @ApiOperation("新增分成比例")
    @PreAuthorize("@ss.hasPermi('module:split:add')")
    @Log(title = "分成比例", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PlatformSplit platformSplit) {
        return toAjax(platformSplitService.save(platformSplit) ? 1 : 0);
    }

    /**
     * 修改分成比例
     */
    @ApiOperation("修改分成比例")
    @PreAuthorize("@ss.hasPermi('module:split:edit')")
    @Log(title = "分成比例", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PlatformSplit platformSplit) {
        return toAjax(platformSplitService.updateById(platformSplit) ? 1 : 0);
    }

    /**
     * 删除分成比例
     */
    @ApiOperation("删除分成比例")
    @PreAuthorize("@ss.hasPermi('module:split:remove')")
    @Log(title = "分成比例" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(platformSplitService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
